/**
 * Copyright (c) 2006 RiseSoft Co.,Ltd  
 * $Header$
 */
package cn.newtest.bi.collection.model;

import java.io.Serializable;
import java.util.List;

import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang.builder.CompareToBuilder;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.GenericGenerator;

/**
 * 直报应用
 *
 * @author <a href="mailto: chenjian@risesoft.net">陈健</a>
 * @version $Revision$
 * 
 */
@Entity
@org.hibernate.annotations.Entity(dynamicInsert=true,dynamicUpdate=true)
@Table(name="rpt_application")
@Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
public class Application implements Comparable<Object>, Serializable ,Cloneable{
	
	private static final long serialVersionUID = 2587332138896110141L;
	
	public static final int STATUS_INITIALIZED = 0;
	public static final int STATUS_DEBUG = 1;
	public static final int STATUS_RUN = 2;
	public static final int STATUS_STOPCOLLECTION = 3;
	public static final int STATUS_COMPLETED = 4;
	
	public static final int USERTYPE_ANONYMOUS = 0;
	public static final int USERTYPE_PASSIVE = 1;
	public static final int USERTYPE_ACTIVE = 2;

	@Id
	@Column(name="app_guid",length=38)
	@GenericGenerator(name="Application-assigned",strategy="assigned")
	private String guid;
	
	@Column(name="App_Name",length=40,nullable=false)
	private String name;
	
	@Column(name="App_ChineseName",length=128,nullable=false)
	private String chineseName;
	
	@Column(name="App_Description",length=255)
	private String description;
	
	@Column(name="App_Type")
	private Integer type;
	
	@Column(name="app_status")
	private Integer status;
	
	@Column(name="app_usertype")
	private Integer userType;
	
	@Column(name="app_inner")
	private Boolean inner;
	
	@Basic
	private Double tabIndex;
	
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="Service_Guid")
	private Service service;
	
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="Speciality_GUID")
	private Speciality speciality;
	
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="App_defaultSoTree")
	private StatisticalObjectTree defaultSoTree;
	
	@OneToMany(cascade=CascadeType.REMOVE,fetch=FetchType.LAZY,mappedBy="application")
	@OrderBy("tabIndex")
	private List<Template> templates;
	
	@OneToMany(cascade=CascadeType.REMOVE,fetch=FetchType.LAZY,mappedBy="application")
	@OrderBy("tabIndex")
	private List<StatisticalObjectTree> soTrees;
	
	public Service getService() {
		return service;
	}

	public void setService(Service service) {
		this.service = service;
	}
	
	@Transient
	public String getServiceGuid() {
		if(service != null){
			return service.getGuid();
		}
		return "";
	}
	public void setServiceGuid(String serviceGuid) {
		if(serviceGuid != null && serviceGuid.trim().length()>0){
			if(service==null){
				service = new Service();				
			}
			service.setGuid(serviceGuid);			
		}
	}
	
	public Speciality getSpeciality() {
		return speciality;
	}

	public void setSpeciality(Speciality speciality) {
		this.speciality = speciality;
	}
	
	@Transient
	public String getSpecialityGuid() {
		if(speciality != null){
			return speciality.getGuid();
		}
		return "";
	}
	
	public void setSpecialityGuid(String specialityGuid) {
		if(specialityGuid != null && specialityGuid.trim().length()>0){
			if(speciality==null){
				speciality = new Speciality();				
			}
			speciality.setGuid(specialityGuid);			
		}
	}
	
	@Transient
	public String getSoTreeGuid() {
		if(defaultSoTree != null){
			return defaultSoTree.getGuid();
		}
		return "";
	}
	public void setSoTreeGuid(String soTreeGuid) {
		if(soTreeGuid != null && soTreeGuid.trim().length()>0){
			if(defaultSoTree==null){
				defaultSoTree = new StatisticalObjectTree();
			}
			defaultSoTree.setGuid(soTreeGuid);
		}
	}
	
	public Integer getStatus() {
		return status;
	}
	public void setStatus(Integer status) {
		this.status = status;
	}
	
	public Integer getUserType() {
		return userType;
	}
	public void setUserType(Integer userType) {
		this.userType = userType;
	}
	
	public Boolean getInner() {
		return inner;
	}
	public void setInner(Boolean inner) {
		this.inner = inner;
	}

	public List<StatisticalObjectTree> getSoTrees() {
		return soTrees;
	}

	public void setSoTrees(List<StatisticalObjectTree> soTrees) {
		this.soTrees = soTrees;
	}

	public List<Template> getTemplates() {
		return templates;
	}

	public void setTemplates(List<Template> templates) {
		this.templates = templates;
	}
	
	public StatisticalObjectTree getDefaultSoTree() {
		return defaultSoTree;
	}

	public void setSoTree(StatisticalObjectTree defaultSoTree) {
		this.defaultSoTree = defaultSoTree;
	}

	public String getChineseName() {
		return chineseName;
	}

	public void setChineseName(String chineseName) {
		this.chineseName = chineseName;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getGuid() {
		return guid;
	}

	public void setGuid(String guid) {
		this.guid = guid;
	}

	public Integer getType() {
		return type;
	}

	public void setType(Integer type) {
		this.type = type;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Double getTabIndex() {
		return tabIndex;
	}

	public void setTabIndex(Double tabIndex) {
		this.tabIndex = tabIndex;
	}

	/**
	 * @see java.lang.Comparable#compareTo(Object)
	 */
	public int compareTo(Object object) {
		Application myClass = (Application) object;
		return new CompareToBuilder().append(this.type, myClass.type).append(this.description, myClass.description)
				.append(this.chineseName, myClass.chineseName).append(this.guid, myClass.guid).append(this.tabIndex, myClass.tabIndex).append(this.name, myClass.name).toComparison();
	}

	/**
	 * @see java.lang.Object#equals(Object)
	 */
	public boolean equals(Object object) {
		if (!(object instanceof Application)) {
			return false;
		}
		Application rhs = (Application) object;
		return new EqualsBuilder().appendSuper(super.equals(object)).append(this.type, rhs.type).append(this.description,
				rhs.description).append(this.chineseName, rhs.chineseName).append(this.guid, rhs.guid).append(this.tabIndex, rhs.tabIndex).append(this.name, rhs.name).isEquals();
	}

	/**
	 * @see java.lang.Object#hashCode()
	 */
	public int hashCode() {
		return new HashCodeBuilder(-1675691591, 1502637525).appendSuper(super.hashCode()).append(this.type).append(this.description).append(this.chineseName).append(this.guid)
				.append(this.tabIndex).append(this.name).toHashCode();
	}

	/**
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		return new ToStringBuilder(this).append("chineseName", this.chineseName).append("projectRelated", this.type).append("tabIndex", this.tabIndex).append("name", this.name).append("description", this.description).append("guid", this.guid).toString();
	}
	
	public Application cloneNoPersistence() throws Exception {
		Application application = (Application) this.clone();
		application.setService(null);
		application.setSoTree(null);
		application.setSoTrees(null);
		application.setSpeciality(null);
		application.setTemplates(null);
		return application;
	}
}